package com.huiruan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiruan.entity.SysRole;
import com.huiruan.entity.SysRoleMenu;
import com.huiruan.service.SysRoleMenuService;
import com.huiruan.service.SysRoleService;
import com.huiruan.mapper.SysRoleMapper;
import com.huiruan.vo.RoleVO;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author CTL
* @description 针对表【sys_role】的数据库操作Service实现
* @createDate 2025-07-09 21:10:46
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{
    private SysRoleMenuService roleMenuService;

    /**
     * 通过用户ID查询角色信息
     * @param userId 用户ID
     * @return 角色信息列表
     */
    @Override
    public List listRolesByUserId(Long userId) {
        return baseMapper.listRolesByUserId(userId);
    }

    /**
     * 根据角色ID查询角色列表
     * @param roleIdList 角色ID列表
     * @param key 缓存key
     * @return 角色列表
     */
    @Override
   // @Cacheable(value = CacheConstants.ROLE_DETAILS, key = "#key", unless = "#result.isEmpty()")
    public List<SysRole> listRolesByRoleIds(List<Long> roleIdList, String key) {
        return baseMapper.selectBatchIds(roleIdList);
    }

    /**
     * 通过角色ID删除角色并清空角色菜单缓存
     * @param ids 角色ID数组
     * @return 删除是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeRoleByIds(Long[] ids) {
        roleMenuService
                .remove(Wrappers.<SysRoleMenu>update().lambda().in(SysRoleMenu::getRoleId, CollUtil.toList(ids)));
        return this.removeBatchByIds(CollUtil.toList(ids));
    }

    /**
     * 更新角色菜单列表
     * @param roleVo 包含角色ID和菜单ID列表的角色对象
     * @return 更新是否成功
     */
//    @Override
//    public Boolean updateRoleMenus(RoleVO roleVo) {
//        return roleMenuService.saveRoleMenus(roleVo.getRoleId(), roleVo.getMenuIds());
//    }


}




